<template>
  <div>
    <div class="dynamic-wrap" style="max-height: 300px;">
      <div v-for="item in keysList" :key="item">
        <a-row :gutter="24">
          <a-col :xs="24" :sm="12">
            <a-form-item label="字典项编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入字典项编码"
                v-decorator="[
                  `${title}dictCode[${item}]`,
                  {
                    initialValue: arr[item] ? arr[item].dictCode : undefined,
                    rules: [
                      {
                        required: true,
                        message: '请输入字典项编码!'
                      }
                    ]
                  }
                ]"
                style="width: 80%; margin-right: 8px" />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="字典项文本" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入字典项文本"
                v-decorator="[
                  `${title}itemText[${item}]`,
                  {
                    initialValue: arr[item] ? arr[item].itemText : undefined,
                    rules: [
                      {
                        required: true,
                        message: '请输入字典项文本!'
                      }
                    ]
                  }
                ]"
                style="width: 80%; margin-right: 8px" />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="字典项值" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入字典项值"
                v-decorator="[
                  `${title}itemValue[${item}]`,
                  {
                    initialValue: arr[item] ? arr[item].itemValue : undefined,
                    rules: [
                      {
                        required: true,
                        message: '请输入字典项值!'
                      }
                    ]
                  }
                ]"
                style="width: 80%; margin-right: 8px" />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="描述信息" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入描述信息"
                v-decorator="[
                  `${title}description[${item}]`,
                  {
                    initialValue: arr[item] ? arr[item].description : undefined,
                    rules: [
                      {
                        required: false,
                        message: '请输入字典项值!'
                      }
                    ]
                  }
                ]"
                style="width: 80%; margin-right: 8px" />
            </a-form-item>
          </a-col>

          <a-col :xs="24" :sm="24">
            <!-- :labelCol="labelCol" :wrapperCol="wrapperCol" -->
            <a-form-item class="RowBtn">
              <a-button
                type="danger"
                ghost
                class="deleteRowBtn"
                v-if="keysList.length > 1"
                @click="() => removeRow(item)"
                icon="delete">
                删除
              </a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </div>
      <a-row>
        <!-- :offset="3" -->
        <a-col :xs="24" :sm="24">
          <a-form-item class="RowBtn">
            <a-button type="dashed" class="addRowBtn" @click="addRow" icon="plus">新增</a-button>
          </a-form-item>
        </a-col>
      </a-row>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
// import { AllCOUNTRY } from './country-constant'
export default {
  name: 'DynamicForm',
  props: {
    title: {
      type: String,
      default: ''
    },
    hasAddress: {
      type: Boolean,
      default: false
    },
    arr: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      // allCountry: AllCOUNTRY,
      id: 0,
      keysList: [],
      moment
    }
  },
  created () {
    this.form = this.$form.createForm(this)
    this.init()
  },
  methods: {
    // 初始化
    init () {
      const arr = [0]
      if (this.arr.length !== 0) {
        for (let i = 1; i < this.arr.length; i++) {
          arr.push(i)
          this.id = this.id + 1
        }
      }
      console.log(this.arr)
      this.keysList = arr
    },
    // 移除某行
    removeRow (k) {
      if (this.keysList.length === 1) {
        // 如果存在可以移除所有行的情况，把条件改为this.keysList.length === 0即可
        return
      }
      this.keysList = this.keysList.filter(item => item !== k)
    },
    // 新增一行
    addRow () {
      this.id = this.id + 1
      this.keysList = this.keysList.concat(this.id)
    }
    // filterOption(input, option) {
    //   return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    // }
  }
}
</script>
<style lang="less" scoped>
.dynamic-wrap {
  padding-top: 10px;
  background-color: white;
  overflow-y: scroll;
  overflow-x: hidden;

  &::-webkit-scrollbar {
    width: 7px;
  }

  &::-webkit-scrollbar-thumb {
    background: #d8d8d8;
    border-radius: 10px;
  }

  &::-webkit-scrollbar-track-piece {
    background: transparent;
  }
}

.minusRowBtn {
  color: #f5222d;
  background: #fff1f0;
  border-color: #ffa39e;
  padding-right: 7px;
  padding-left: 7px;
  height: 29px;
  margin-left: 10px;
}

.deleteRowBtn {
  // width: 120%;
  // margin-left: 20px;

  width: 516px;
}

.addRowBtn {
  // width: 80%;
  color: #1890ff;
  border-color: #91d5ff;
  width: 516px;
  // margin-left: -3px;
}

.RowBtn {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}</style>
